package com.whzm.task;

import com.whzm.mapper.MemberInfoMapper;
import com.whzm.pojo.MemberInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;

@Component
@Configuration
@EnableScheduling
@Slf4j
public class ChangeStatus {
    @Autowired
    MemberInfoMapper memberInfoMapper;
    @Scheduled(cron = "0 0 0 * * ?")
    //@Scheduled(fixedRate = 5000)
    public void Task1(){
        //当前时间
        LocalDate now = LocalDate.now();
        //查询所有会员信息，定时修改已过期的会员信息的状态
        List<MemberInfo> lists=memberInfoMapper.getlist();
        for(MemberInfo memberInfo:lists){
            if(!StringUtils.isEmpty(memberInfo)) {
                LocalDate oldDate = LocalDate.parse(memberInfo.getExpirationTime());
                //如果过期，修改状态
                if (now.isAfter(oldDate)) {
                    //修改会员为失效状态
                    memberInfo.setStatus("0");
                    int i = memberInfoMapper.updateMemberInfo(memberInfo);
                }
            }
        }
    }
}
